//
//  STRefreshCollectionView.m
//  赛拓
//
//  Created by 王磊 on 7/28/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STRefreshCollectionView.h"
#import "STBannerView.h"
#import "STHeaderView.h"
#import "STCommodityBean.h"
#import "STCommdityCollectionViewCell.h"
#import "STSellerListCollectionViewCell.h"
#import "STHomeBean.h"
#import "STBaseTableViewCell.h"
#import "STAdvertisementCollectionViewCell.h"
@interface STRefreshCollectionView()<STBaseViewDelegate>

@property (nonatomic ,strong) STHeaderView *headerView;

@property (nonatomic ,strong) NSTimer *timer;

@property (nonatomic ,strong) STHomeBean *home;

@end
static NSString *headerId = @"headerId";
static NSString *commodityCellIdentifier = @"commodity_cell";
static NSString *sellerCellIdentifier = @"seller_cell";
static NSString *addvertisementCellIdentifier = @"addvertisementCellIdentifier";
static NSString *clearHeaderIdentifier = @"clearHeaderIdentifier";
@implementation STRefreshCollectionView


- (void)commitInit {
    [super commitInit];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    header.automaticallyChangeAlpha = YES;
    
    [header setValue:@(1) forKeyPath:@"lastUpdatedTimeLabel.hidden"];
    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [header setTitle:@"松开载入" forState:MJRefreshStatePulling];
    [header setTitle:@"正在载入..." forState:MJRefreshStateRefreshing];
    [header setTitle:@"正在载入..." forState:MJRefreshStateWillRefresh];
    [header setTitle:@"暂无更多数据" forState:MJRefreshStateNoMoreData];
    
    [header setValue:ST_BLACK_COLOR forKeyPath:@"stateLabel.textColor"];
    header.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    self.mj_header = header;
    
    [self.mj_header beginRefreshing];
    
    [self registerClass:[STHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
    
    [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:clearHeaderIdentifier];
    
    [self registerClass:[STAdvertisementCollectionViewCell class] forCellWithReuseIdentifier:addvertisementCellIdentifier];
    
    [self registerClass:[STCommdityCollectionViewCell class] forCellWithReuseIdentifier:commodityCellIdentifier];
    
    [self registerClass:[STSellerListCollectionViewCell class] forCellWithReuseIdentifier:sellerCellIdentifier];
    
    self.bounces = true;
}
- (void)loadNewData {
    
    [self.mDelegate loadNewData];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return section == 0 ? self.home.hots.count : section == 1 ? self.home.bads.count : self.home.hotsSeller.count;
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSInteger section = 0;
    if (self.home.hotsSeller.count) {
        section += 1;
    }
    if (self.home.bads.count) {
        section += 1;
    }
    if (self.home.hots.count) {
        
        section += 1;
    }
    return section;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        STCommdityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:commodityCellIdentifier forIndexPath:indexPath];
        
        cell.mDelegate = self;
        
        [cell updateData:self.home.hots[indexPath.row]];
        
        return cell;
        
    } else if (indexPath.section == 1){
        
        STAdvertisementCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:addvertisementCellIdentifier forIndexPath:indexPath];
        
        [cell updateData:self.home.bads[indexPath.row]];
        
        return cell;
    } else {
        
        STSellerListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:sellerCellIdentifier forIndexPath:indexPath];
        
        cell.mDelegate = self;
        
        [cell updateData:self.home.hotsSeller[indexPath.row]];
        
        return cell;
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            STHeaderView * view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId forIndexPath:indexPath];
            
            [view updateData:self.home];
            
            view.mDelegate = self;
            
            return view;
        } else {
            
            return nil;
        }
    } else {
        
        UICollectionReusableView * view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:clearHeaderIdentifier forIndexPath:indexPath];
        
        view.backgroundColor = ST_CLEAR_COLOR;
        
        return view;
    }
}
- (void)updateHeader:(id)data {
    
    self.home = data;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(ST_SCREEN_WIDTH, [STHeaderView headerViewHeight:self.home]);
    } else {
        return CGSizeMake(ST_SCREEN_WIDTH, 20 * ST_RATE);
    }
}
- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    
    [self.mDelegate onItemClick:itemType withData:data forIndexPath:indexPath];
    
}

#pragma mark --- collection view delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:true];
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(onItemClick:withData:forIndexPath:)]) {
        
        [self.mDelegate onItemClick:STItemClickTypeItem withData:self.home forIndexPath:indexPath];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        return CGSizeMake((ST_SCREEN_WIDTH - 20 * ST_RATE) / 2, 450 * ST_RATE);
        
    }else if (indexPath.section == 1) {
        
        return CGSizeMake(ST_SCREEN_WIDTH, ST_SCREEN_WIDTH / 4);
        
    } else {
        
        return CGSizeMake(ST_SCREEN_WIDTH, [STBaseTableViewCell heightForCell:STCellTypeSellerList withData:self.home.hotsSeller[indexPath.row] forIndexPath:indexPath]);
    }
}
- (void)updateData:(id)data {
    
    self.home = data;
    
    [self reloadData];
}
@end